var map;
var operator;
var browserSupportFlag = new Boolean();
var initialLocation = createPoint(47.24095820891846, 6.024799346923828);
var contextMenu;

function getOptions() {
    return {
        center: initialLocation,
        zoom: 15
    };
}

function initialize_admin_map(operator_) {
    init_admin_table_reporting(function(data) { 
        adminReport(data);
        map.panTo(createPoint(data[3], data[4]));
        map.setZoom(18);
    });
    init_restitution_table_reporting(function(data) {  
        map.panTo(createPoint(data[1], data[2])); 
        map.setZoom(18);
    });
    init_itineraire_table_reporting();
    operator = operator_;
    initialize_admin_map_("admin_map-canvas")
    loadGlobalZone(map, operator);
    loadRestitutionZones(map, operator);
    loadSmartlock(load_admin_table_reporting);
}

function initialize_admin_map_(canvas) {
    var options = getOptions();
    loadMapOnElement(options, canvas);
    /*
     var bikeLayer = new google.maps.BicyclingLayer();
     bikeLayer.setMap(map);
     */
    directionsDisplay.setMap(map);

}

function createPoint(lat, lng) {
    return new google.maps.LatLng(lat, lng);
}
function createMapContextualMenu() {
    //	create the ContextMenuOptions object
    var contextMenuOptions = {};
    contextMenuOptions.classNames = {menu: 'context_menu', menuSeparator: 'context_menu_separator'};

    //	create an array of ContextMenuItem objects
    var menuItems = [];
    menuItems.push({});
    menuItems.push({className: 'context_menu_item', eventName: 'findSmartlock', label: 'Trouver un SL online'});
    menuItems.push({className: 'context_menu_item', eventName: 'findSmartlockFly', label: 'Trouver un SL offline'});
    //	a menuItem with no properties will be rendered as a separator
    menuItems.push({});
    menuItems.push({className: 'context_menu_item', eventName: 'streetView', label: 'Vue depus la rue'});
    menuItems.push({className: 'context_menu_item', eventName: 'center_map_click', label: 'Centrer ici'});
    contextMenuOptions.menuItems = menuItems;

    createEmptyContextualMenu();
    contextMenu = new ContextMenu(map, contextMenuOptions); 
    //	display the ContextMenu on a Map right click
    google.maps.event.addListener(map, 'rightclick', function(mouseEvent) {
        contextMenu.show(mouseEvent.latLng);
    });

    //	listen for the ContextMenu 'menu_item_selected' event
    google.maps.event.addListener(contextMenu, 'menu_item_selected', function(latLng, eventName) {
        //	latLng is the position of the ContextMenu
        //	eventName is the eventName defined for the clicked ContextMenuItem in the ContextMenuOptions 
        switch (eventName) {
            case 'center_map_click':
                map.panTo(latLng);
                break;
            case 'streetView':
                var panorama = map.getStreetView();
                panorama.setPosition(latLng);
                panorama.setPov(/** @type {google.maps.StreetViewPov} */({
                    heading: 265,
                    pitch: 0
                }));
                panorama.setVisible(true);
                break;
            case 'findSmartlock':
                computeClosestSmartlock(latLng, 'online');
                break;
            case 'findSmartlockFly':
                computeClosestSmartlock(latLng, 'offline');
                break;
        }
    });
}


function createRestrictionZoneContextualMenu() {
    //	create the ContextMenuOptions object
    var contextMenuOptions = {};
    contextMenuOptions.classNames = {menu: 'context_menu', menuSeparator: 'context_menu_separator'};

    //	create an array of ContextMenuItem objects
    var menuItems = [];
    menuItems.push({className: 'context_menu_item', eventName: 'addRestitutionBound', label: 'Placer une zone de restitution'});

    //	a menuItem with no properties will be rendered as a separator
    menuItems.push({});
    menuItems.push({className: 'context_menu_item', eventName: 'streetView', label: 'Vue depus la rue'});
    menuItems.push({className: 'context_menu_item', eventName: 'center_map_click', label: 'Centrer ici'});
    contextMenuOptions.menuItems = menuItems;

    createEmptyContextualMenu();
    contextMenu = new ContextMenu(map, contextMenuOptions);
 
    //	display the ContextMenu on a Map right click
    google.maps.event.addListener(map, 'rightclick', function(mouseEvent) {
        contextMenu.show(mouseEvent.latLng);
    });

    //	listen for the ContextMenu 'menu_item_selected' event
    google.maps.event.addListener(contextMenu, 'menu_item_selected', function(latLng, eventName) {
        //	latLng is the position of the ContextMenu
        //	eventName is the eventName defined for the clicked ContextMenuItem in the ContextMenuOptions 
        switch (eventName) {
            case 'addRestitutionBound':
                newMarkersRestitutionZone(latLng.lat(), latLng.lng());
                break;
            case 'center_map_click':
                map.panTo(latLng);
                break;
            case 'streetView':
                var panorama = map.getStreetView();
                panorama.setPosition(latLng);
                panorama.setPov(/** @type {google.maps.StreetViewPov} */({
                    heading: 265,
                    pitch: 0
                }));
                panorama.setVisible(true);
                break;
        }
    });
}
 
function createEmptyContextualMenu() {
    google.maps.event.clearListeners(map, 'rightclick');
}

function createGlobalZoneContextualMenu() {
    //	create the ContextMenuOptions object
    var contextMenuOptions = {};
    contextMenuOptions.classNames = {menu: 'context_menu', menuSeparator: 'context_menu_separator'};

    //	create an array of ContextMenuItem objects
    var menuItems = [];
    menuItems.push({className: 'context_menu_item', eventName: 'addGlobalBound', label: 'Placer une borne de zone globale'});
    menuItems.push({});
    menuItems.push({className: 'context_menu_item', eventName: 'streetView', label: 'Vue depus la rue'});
    menuItems.push({className: 'context_menu_item', eventName: 'center_map_click', label: 'Centrer ici'});
    contextMenuOptions.menuItems = menuItems;

    createEmptyContextualMenu();
    var contextMenu = new ContextMenu(map, contextMenuOptions);

    //	display the ContextMenu on a Map right click
    google.maps.event.addListener(map, 'rightclick', function(mouseEvent) {
        contextMenu.show(mouseEvent.latLng);
    });

    //	listen for the ContextMenu 'menu_item_selected' event
    google.maps.event.addListener(contextMenu, 'menu_item_selected', function(latLng, eventName) {
        //	latLng is the position of the ContextMenu
        //	eventName is the eventName defined for the clicked ContextMenuItem in the ContextMenuOptions 
        switch (eventName) {
            case 'addGlobalBound':
                newMarkersGlobalZone(latLng.lat(), latLng.lng());
                break;
            case 'center_map_click':
                map.panTo(latLng);
                break;
            case 'streetView':
                var panorama = map.getStreetView();
                panorama.setPosition(latLng);
                panorama.setPov(/** @type {google.maps.StreetViewPov} */({
                    heading: 265,
                    pitch: 0
                }));
                panorama.setVisible(true);
                break;
        }
    });
}
 
function loadMapOnElement(options, canvas) {
    map = new google.maps.Map(document.getElementById(canvas),
            options);
}


